<template>
  <div class="login-container">
    <div class="login-form">
      <h2>家校共创系统登录</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">用户名:</label>
          <input type="text" id="username" v-model="loginForm.username" required />
        </div>
        
        <div class="form-group">
          <label for="password">密码:</label>
          <input type="password" id="password" v-model="loginForm.password" required />
        </div>
        
        <button type="submit" class="login-btn">登录</button>
      </form>
      
      <div class="register-link">
        <p>还没有账号? <router-link to="/register">立即注册</router-link></p>
      </div>
      
      <div v-if="error" class="error-message">
        {{ error }}
      </div>
    </div>
  </div>
</template>

<script>
import { ref } from 'vue';
import { useRouter } from 'vue-router';

export default {
  name: 'Login',
  setup() {
    const router = useRouter();
    const error = ref('');
    
    const loginForm = ref({
      username: '',
      password: ''
    });
    
    const handleLogin = async () => {
      try {
        // 进行登录
        const loginResponse = await fetch('/user/login', {
          method: 'POST',
          credentials: 'include',
          headers: {
            'Content-Type': 'application/json'
          },
          body: JSON.stringify(loginForm.value)
        });
        
        const loginResult = await loginResponse.json();
        if (loginResult.code === 1) {
          // 登录成功，根据返回的用户类型跳转到不同页面
          switch (loginResult.data) {
            case '1': // 教师
              router.push('/teacher/dashboard');
              break;
            case '2': // 学生
              // 学生用户需要先获取详细信息以获取学号
              const profileResponse = await fetch('/user/profile', {
                method: 'GET',
                credentials: 'include',
                headers: {
                  'Content-Type': 'application/json'
                }
              });
              
              const profileResult = await profileResponse.json();
              if (profileResult.code === 1) {
                // 保存学号到本地存储
                if (profileResult.data.studentNumber) {
                  localStorage.setItem('studentNumber', profileResult.data.studentNumber);
                }
              }
              router.push('/student/dashboard');
              break;
            case '3': // 家长
              router.push('/parent/dashboard');
              break;
            case '4': // 管理员
              router.push('/admin/dashboard');
              break;
            default:
              router.push('/student/dashboard');
          }
        } else {
          error.value = loginResult.msg || '登录失败';
        }
      } catch (err) {
        error.value = '网络错误，请稍后重试';
      }
    };
    
    return {
      loginForm,
      error,
      handleLogin
    };
  }
};
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  background-color: #f5f5f5;
}

.login-form {
  background: white;
  padding: 2rem;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
  width: 100%;
  max-width: 400px;
}

.login-form h2 {
  text-align: center;
  margin-bottom: 1.5rem;
  color: #333;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: bold;
  color: #555;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 0.75rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 1rem;
  box-sizing: border-box;
}

.login-btn {
  width: 100%;
  padding: 0.75rem;
  background-color: #409EFF;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 1rem;
  cursor: pointer;
  margin-top: 1rem;
}

.login-btn:hover {
  background-color: #337ecc;
}

.register-link {
  text-align: center;
  margin-top: 1rem;
}

.register-link a {
  color: #409EFF;
  text-decoration: none;
}

.register-link a:hover {
  text-decoration: underline;
}

.error-message {
  background-color: #f8d7da;
  color: #721c24;
  padding: 0.75rem;
  border-radius: 4px;
  margin-top: 1rem;
}
</style>